/**
****************************************************************************************
 * @Author: luoweiyong fortwater969@163.com
 * @Version: 
 * @Date: 2024-06-26 23:36:33
 * @LastEditors: luoweiyong fortwater969@163.com
 * @LastEditTime: 2024-06-26 23:36:34
 * @FilePath: \FOC_F103C8T6\USER\ws2812b\ws2812b.c
 * @Description: 
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
****************************************************************************************
*/
#include <stdlib.h>

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "smarttimer.h"
#include "ws2812b.h"

#define WS2812_LED_NUM  5

uint8_t ws2812_data_buffer[WS2812_LED_NUM][24];

static void ws2812_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

static void ws2812_SPI_Init(void)
{
    SPI_InitTypeDef SPI_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

    SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI1, &SPI_InitStructure);

    SPI_Cmd(SPI1, ENABLE);
    SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
}

static void ws2812_DMA_Init(void)
{
    DMA_InitTypeDef DMA_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    DMA_DeInit(DMA1_Channel3);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI1 -> DR);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ws2812_data_buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = WS2812_LED_NUM * 24;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; 
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(DMA1_Channel3, &DMA_InitStructure);
}

void ws2812_Send_Data(void)
{
    DMA_Cmd(DMA1_Channel3, DISABLE );
    DMA_ClearFlag(DMA1_FLAG_TC3);
    DMA_SetCurrDataCounter(DMA1_Channel3,24 * WS2812_LED_NUM);
    DMA_Cmd(DMA1_Channel3, ENABLE);
}

void ws2812_AllShutOff(void)
{
    uint16_t i = 0;
    uint8_t j = 0;

    for(i = 0; i < WS2812_LED_NUM; i++)
    {
        for(j = 0; j < 24; j++)
        {
            ws2812_data_buffer[i][j] = SIG_0;
        }
    }
    ws2812_Send_Data();
    stim_delay(10 * WS2812_LED_NUM);
}

void ws2812_Init(void)
{
    ws2812_GPIO_Init();
    ws2812_SPI_Init();
    ws2812_DMA_Init();
    ws2812_AllShutOff();
    stim_delay(WS2812_LED_NUM * 10);
}

uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
    return green << 16 | red << 8 | blue;
}


void ws2812_AllOpen(uint8_t red, uint8_t green, uint8_t blue)
{
    uint16_t i = 0;
    uint16_t j = 0;

    for(j = 0; j < WS2812_LED_NUM; j++)
    {
        for(i = 0; i < 24; ++i)
        {
            ws2812_data_buffer[j][i] = (((ws281x_color(red, green, blue) << i) & 0X800000) ? SIG_1 : SIG_0);
        }
    }
    ws2812_Send_Data();
    stim_delay(10);
}

void ws2812_Set_one_LED_Color(uint16_t LED_index, uint32_t GRB_color)
{
    uint8_t i = 0;
    uint32_t cnt = 0x800000;
    if(LED_index < WS2812_LED_NUM)
    {
        for(i = 0; i < 24; ++i)
        {
            if(GRB_color & cnt)
            {
                ws2812_data_buffer[LED_index][i] = SIG_1;
            }
            else
            {
                ws2812_data_buffer[LED_index][i] = SIG_0;
            }
            cnt >>= 1;
        }
    }
}

uint32_t ws2812_LED_Gray2GRB(uint8_t LED_gray)
{
    LED_gray = 0xFF - LED_gray;
    if(LED_gray < 85)
    {
        return (((0xFF - 3 * LED_gray)<<8) | (3 * LED_gray));
    }
    if(LED_gray < 170)
    {
        LED_gray = LED_gray - 85;
        return (((3 * LED_gray)<<16) | (0xFF - 3 * LED_gray));
    }
    LED_gray = LED_gray - 170;
    return (((0xFF - 3 * LED_gray)<<16) | ((3 * LED_gray)<<8));
}

/*WS2812 灰度值驱动渐变效果 沿色环转动*/
/*{uint16_t interval_time} 渐变间隔时间*/
void ws2812_Roll_on_Color_Ring(uint16_t interval_time)
{
    uint8_t i = 0;
    uint16_t j = 0;
    for(i = 0;i <= 255;i++)
    {
        for(j = 0; j < WS2812_LED_NUM;j++)
        {
            ws2812_Set_one_LED_Color(j, ws2812_LED_Gray2GRB(i));
        }
        ws2812_Send_Data();
        stim_delay(interval_time);
    }
}

/**
 * @Description  	WS2812 设置第n个灯珠的颜色
* @Param     n:第几个灯珠   red:0-255   green:0-255    blue:0-255 	   eg:yellow:255 255 0
 * @Return    	  
*/

//设定第n个灯珠的颜色
void ws281x_setPixelRGB(uint16_t n, uint8_t red, uint8_t green, uint8_t blue)
{
    uint8_t i = 0;

    if(n < WS2812_LED_NUM)
    {
        for(i = 0; i < 24; ++i)
        {
            ws2812_data_buffer[n][i] = (((ws281x_color(red,green,blue) << i) & 0X800000) ? SIG_1 : SIG_0);
        }
    }
    ws2812_Send_Data();
    stim_delay(10);
}

/**
*@Description   流水灯效果
*@Param         interval_time:间隔时间  red:0-255 green:0-255 blue:0-255
*@Return    	NONE
*/
void Running_water_lamp( uint8_t red, uint8_t green, uint8_t blue, uint16_t interval_time)
{
    uint16_t i = 0;

    for(i = 0; i < WS2812_LED_NUM; i++)
    {
        ws281x_setPixelRGB(i, red, green, blue);
        stim_delay(interval_time);
    }
    ws2812_AllShutOff();
    stim_delay(interval_time);
}

/**
 * @Description  	WS2812 设置灯珠颜色（固定的）
* @Param       		 n:第几个灯珠   color:哪种颜色（0-7）
 * @Return    	  
*/
void set_pixel_rgb(uint16_t n,u8 color)
{
    switch(color)
    {
        case Red: 
            ws281x_setPixelRGB(n, 255, 0, 0);
            break;
        case Green: 
            ws281x_setPixelRGB(n, 0, 255, 0);
            break;
        case Blue: 
            ws281x_setPixelRGB(n, 0, 0, 255);
            break;
        case Yellow: 
            ws281x_setPixelRGB(n, 255, 255, 0);
            break;
        case Purple: 
            ws281x_setPixelRGB(n, 255, 0, 255);
            break;
        case Orange: 
            ws281x_setPixelRGB(n, 255, 125, 0);
            break;
        case Indigo: 
            ws281x_setPixelRGB(n, 0, 255, 255);
            break;
        case White:
            ws281x_setPixelRGB(n, 255, 255, 255);
            break;
        default:
            break;
    }
}

//设置关闭第n个灯珠
void ws281x_ShutoffPixel(uint16_t n)
{
    uint8_t i;

    if(n < WS2812_LED_NUM)
    {
        for(i = 0; i < 24; ++i)
        {
            ws2812_data_buffer[n][i] = SIG_0;
        }
    }
    ws2812_Send_Data();
    stim_delay(10);
}


/**
 * @Description  	跑马灯效果
* @Param     interval_time:间隔时间
 * @Return    	NONE  
*/
void horse_race_lamp(uint16_t interval_time)
{
    uint8_t i = 0;
    uint8_t color = 0;

    for(i = 0; i < WS2812_LED_NUM; i++)
    {
        color = rand()%7;
        set_pixel_rgb(i, color);//随机颜色
        ws281x_ShutoffPixel(i - 1);
        stim_delay(interval_time);
    }
    ws281x_ShutoffPixel(WS2812_LED_NUM-1);
    stim_delay(interval_time);
}

void fix_horse_race_lamp(void)
{
    uint8_t i = 0;
    uint8_t color = 0;

    for(i = 0; i < WS2812_LED_NUM; i++)
    {
        color = rand()%7;
        set_pixel_rgb(i, color);//随机颜色
        ws281x_ShutoffPixel(i - 1);
        stim_delay(2000);
    }
    ws281x_ShutoffPixel(WS2812_LED_NUM-1);
    stim_delay(2000);
}


